<template>
    <div>
         <!-- 新建或者修改对话框 -->
        <el-dialog
            title="品牌"
            :visible.sync="dialogVisible"
            width="30%">
            {{trademark}}
            <el-form label-width="120px">
                <el-form-item label="标题">
                    <el-input v-model="trademark.tmName"/>
                </el-form-item>

                <el-form-item label="Logo">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8086/admin/product/fileUpload"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="trademark.logoUrl" :src="trademark.logoUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                {{multiFileList}}

                <el-form-item label="多文件">
                    <el-upload
                        action="http://localhost:8086/admin/product/fileUpload"
                        list-type="picture-card"
                        :on-success="handlePictureCardSuccess"
                        :on-remove="handlePictureCardRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>

                {{videoUrl}}
                <el-form-item label="视频">
                    <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8086/admin/product/fileUploadCheckpoint"
                        :show-file-list="false"
                        :on-success="handleVideoSuccess">
                        <video controls v-if="videoUrl"  class="avatar">
                            <source :src="videoUrl" type="video/mp4">
                        </video>
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="save">保存</el-button>
                </el-form-item>
                </el-form>        

        </el-dialog>
    <!-- 工具条 -->
    <div>
      <el-button type="danger" size="mini" @click="addTrademark" >添加</el-button>
    </div>
    {{trademarkList}}
     <!-- 列表 -->
        <el-table
      :data="trademarkList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="50">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="名称"
        width="180">
        <template slot-scope="scope1">
            <el-input v-model="scope1.row.tmName" v-show="scope1.row.isEdit" />
            <el-tag v-show="scope1.row.isEdit==false">{{scope1.row.tmName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="logoUrl"
        label="图片">
        <template slot-scope="scope">
            <el-image v-show="scope.row.isEdit ==false"
                style="width: 100px; height: 100px"
                :src="scope.row.logoUrl">
            </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="修改时间">
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <div v-show = "scope.row.isEdit == false">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row.id)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteById(scope.row.id)"></el-button>
        </div>
        <div v-show = "scope.row.isEdit">
          <el-button type="danger" size="mini" icon="el-icon-check" @click="save1(scope.row)">保存</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="2"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
</template>
<script>
import * as tradeMarkApi from '../../api/product/trademark.js'
export default {
    data(){
        return{
            trademarkList:[],
            pageNo:1,
            pageSize:2,
            total:0,
            dialogVisible:false,
            trademark:{
                tmName:'',
                logoUrl:''
            },
            multiFileList:[],
            videoUrl:''
        }
    },
    methods:{
        findPage(){
            tradeMarkApi.findPage(this.pageNo,this.pageSize)
                .then(res=>{
                    console.log(res);
                    if(res){
                        this.trademarkList = res.data.data.records;
                        this.pageNo = res.data.data.current;
                        this.pageSize = res.data.data.size;
                        this.total = res.data.data.total;
                         this.trademarkList.forEach(tm=>{
                             tm.isEdit = false;
                         })
                    }
                })
        },
        openAddDialog(){
            this.dialogVisible = true;
        },
        addTrademark(){
            this.trademarkList.push({
                isEdit:true
            })
        },
        openEditDialog(id){
            this.dialogVisible = true;
            tradeMarkApi.getById(id).then(res=>{
                if(res.data.ok){
                    this.trademark = res.data.data;
                    this.dialogVisible = true;
                }
            })
        },
        save1(row){
            if(row.id == null){
                tradeMarkApi.save(row).then(res=>{
                    console.log(res.data)
                    if(res.data.ok){
                        alert(res.data.message)
                    }
                })
            }else{
                tradeMarkApi.update(row).then(res=>{
                    console.log(res.data)
                    if(res.data.ok){
                        alert(res.data.message)
                    }
                })  
            }
        },
        save(){
            if(this.trademark.id == null){
                tradeMarkApi.save(this.trademark).then(res=>{
                    console.log(res.data)
                    if(res.data.ok){
                        alert(res.data.message)
                        this.dialogVisible = false;
                    }
                })
            }else{
                tradeMarkApi.update(this.trademark).then(res=>{
                    console.log(res.data)
                    if(res.data.ok){
                        alert(res.data.message)
                        this.dialogVisible = false;
                    }
                })  
            }
        },
        deleteById(id){
            tradeMarkApi.deleteById(id).then(res=>{
                console.log(res.data)
                if(res.data.ok){
                    alert(res.data.message)
                }
            })
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.findPage();
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
             this.pageNo = val;
             this.findPage();
            console.log(`当前页: ${val}`);
        },
        handleVideoSuccess(res,file){//处理视频
            this.videoUrl = res.data;
            console.log(res,file);
        },
        handlePictureCardSuccess(res,file,fileList){//处理照片墙，多图片
              this.multiFileList =[]
            fileList.forEach(element => {
               this.multiFileList.push(element.response.data);
            });
            console.log(res,fileList);
        },
        handlePictureCardRemove(file, fileList){
            this.multiFileList =[]
            fileList.forEach(element => {
               this.multiFileList.push(element.response.data);
            });
            console.log(fileList);
        },
        handleAvatarSuccess(res, file) {//处理头像，单图片
            console.log(res);
            this.trademark.logoUrl = res.data;
        },
        beforeAvatarUpload(file) {
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isPNG) {
                this.$message.error('上传头像图片只能是 png 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isPNG && isLt2M;
        }
    },
    mounted(){
        this.findPage();
    }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>


